//
//  HZLNavigationBar.m
//  HZLRead
//
//  Created by SDMac on 2019/11/28.
//  Copyright © 2019 盛世大联. All rights reserved.
//

#import "HZLNavigationBar.h"
#import "UIView+Hzl.h"
#import "CommonMacro.h"

@interface HZLNavigationBar ()

/** 导航条显示 */
@property (nonatomic, strong) UILabel *titleLabel;

/** 导航条左按钮 */
@property (nonatomic, strong) UIButton *leftButton;

/** 导航条右按钮 */
@property (nonatomic, strong) UIButton *rightButton;

@end

@implementation HZLNavigationBar

#pragma mark - life cycle

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.userInteractionEnabled = YES;
        self.clipsToBounds=YES;
        
        [self addSubview:self.leftButton];
        [self addSubview:self.titleLabel];
        [self addSubview:self.rightButton];
    }
    
    return self;
}

#pragma mark - UITableViewDelegate

#pragma mark - custom delegate

#pragma mark - event response

#pragma mark - public Methods

- (UIButton*)setLeftButtonWithTitle:(NSString*)title action:(SEL)action target:(nullable id)target
{
    [self.leftButton setTitle:title forState:UIControlStateNormal];
    [self.leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return self.leftButton;
}
- (UIButton*)setLeftButtonWithImageName:(NSString*)imageName action:(SEL)action target:(nullable id)target
{
    [self.leftButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [self.leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return self.leftButton;
}
- (UIButton*)setRightButtonWithTitle:(NSString*)title action:(SEL)action target:(nullable id)target
{
    [self.rightButton setTitle:title forState:UIControlStateNormal];
    [self.rightButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    CGSize size = CGSizeMake(MAXFLOAT,self.rightButton.height);
    CGFloat lableW = [title sizeWithFont:self.rightButton.titleLabel.font constrainedToSize:size].width + 30;
    
    self.rightButton.x = hScreenWidth - lableW;
    self.rightButton.width = lableW;
    return self.rightButton;
}
- (UIButton*)setRightButtonWithImageName:(NSString*)imageName action:(SEL)action target:(nullable id)target
{
    [self.rightButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [self.rightButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return self.rightButton;
}

#pragma mark - private Methods

#pragma mark - getters and setters

-(void)setTitle:(NSString *)title
{
    self.titleLabel.text = title;
}

- (UILabel*)titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, hStatusBarHeight, hScreenWidth - 120, hNavBarHeight)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = FONT_SIZE(18);
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _titleLabel;
}
- (UIButton*)leftButton
{
    if (!_leftButton)
    {
        _leftButton = [[UIButton alloc]initWithFrame:CGRectMake(3, hStatusBarHeight, hNavBarHeight, hNavBarHeight)];
        _leftButton.backgroundColor = [UIColor clearColor];
        _leftButton.showsTouchWhenHighlighted = YES;
        _leftButton.titleLabel.font = FONT_SIZE(13);
    }
    
    return _leftButton;
}
- (UIButton*)rightButton
{
    if (!_rightButton)
    {
        _rightButton = [[UIButton alloc]initWithFrame:CGRectMake(hScreenWidth - 70, hStatusBarHeight, 70, hNavBarHeight)];
        _rightButton.backgroundColor = [UIColor clearColor];
        _rightButton.showsTouchWhenHighlighted = YES;
        _rightButton.titleLabel.font = FONT_SIZE(13);
    }
    
    return _rightButton;
}

@end
